package com.njupt.Array;

/**
 * 35. 搜索插入位置
 * 给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标值不存在于数组中，返回它将会被按顺序插入的位置。
 */
public class BinarySearchInsert {


    public int searchInsert(int[] nums, int target) {

        int left = 0;
        int right = nums.length-1;
        int middle = -1;
        while(left<=right){
            middle = (left + right)/2;
            if(target == nums[middle]){
                return middle;
            } else if (target>nums[middle]) {
                left = middle +1;
            }else {
                right = middle-1;
            }
        }
        return left;

    }

    public static void main(String[] args) {
        int[] nums = {1,3,5,6};
        BinarySearchInsert test = new BinarySearchInsert();
        System.out.println(test.searchInsert(nums, 7));
    }
}
